package com.juyu.miliao.api.dao.redis;

import com.juyu.miliao.api.cache.RedisKeyPrefix;
import com.juyu.miliao.api.dao.mysql.QuestionAndAnswerPayInfoDao;
import com.juyu.miliao.api.domain.QuestionAndAnswerPayInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户问答购买Redis
 *
 * @author xuanqicheng
 * @create 2018-19-1:24 PM
 */
@Component
public class QuestionAndAnswerPayInfoRedisDao {

    /**
     * 格式QuestionAndAnswerPay:{问题id}
     */
    private static final String QUESTION_AND_ANSWER_PAY = RedisKeyPrefix.MI_LIAO_API_KEY + "QuestionAndAnswerPay:";

    @Autowired
    private QuestionAndAnswerPayInfoDao questionAndAnswerPayInfoDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 检查用户是否购买过
     *
     * @param questionId
     * @param userId
     * @return
     */
    public boolean checkUserIsBy(Long questionId, long userId) {

        String key = QUESTION_AND_ANSWER_PAY+questionId;

        boolean bl =stringRedisTemplate.opsForSet().isMember(key,userId+"");

//        Map<String, Object> parameMap = new HashMap<>(0);
//        parameMap.put("questionId", questionId);
//        parameMap.put("userId", userId);
//        QuestionAndAnswerPayInfo obj = questionAndAnswerPayInfoDao.selectInfoByQuestionIdAndUserId(parameMap);
//
//        if (obj != null) {
//
//            bl = true;
//        }

        return bl;
    }

    public void updateQuestionAndAnswerPayInfo(long question , long userId){

        String key = QUESTION_AND_ANSWER_PAY+question;

        stringRedisTemplate.opsForSet().add(key,userId+"");
    }
}
